﻿
#region Usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Shapes;
using FasterWPF.Selectors;
#endregion

namespace FasterWPF.Dialogs
{
    public static class DialogHelpers
    {
        /// <summary>
        /// Display a ListBox Dialog. Will NOT display if another dialog with the SAME dialogGuid is already displaying. buildFormAction contains StackPanel for body, okButton Border, cancelButton Border, and title Label.
        /// </summary>
        /// <param name="canVas"></param>
        /// <param name="dialogGuid"></param>
        /// <param name="width"></param>
        /// <param name="internarlGridRowHeight"></param>
        /// <param name="borderWidth"></param>
        /// <param name="borderHeight"></param>
        /// <param name="left"></param>
        /// <param name="top"></param>
        /// <param name="dialogBoxBorder"></param>
        /// <param name="dialogBoxBorderThickness"></param>
        /// <param name="borderBackground"></param>
        /// <param name="listBoxSettings"></param>
        /// <param name="titleSettings"></param>
        /// <param name="labelSettings"></param>
        /// <param name="questionDefaultText"></param>
        /// <param name="listBoxMargin"></param>
        /// <param name="comboBoxBackground"></param>
        /// <param name="comboBoxMaxLength"></param>
        /// <param name="buttonBackgrounds"></param>
        /// <param name="buttonBorders"></param>
        /// <param name="buttonOnMouseOverColors"></param>
        /// <param name="setQuestionBoxFocusType"></param>
        /// <param name="secondsToDelay"></param>
        /// <param name="OKClickAction"></param>
        /// <param name="CANCELClickAction"></param>
        /// <param name="COMMON_CLOSEClickAction"></param>
        /// <returns></returns>
        public static Border ShowListBoxDialog(Canvas canVas, string dialogGuid, double width, double internarlGridRowHeight, double listBoxRowHeight, double borderWidth, double borderHeight, double left, double top, Brush dialogBoxBorder, Thickness dialogBoxBorderThickness, Brush borderBackground, ListBoxSettings listBoxSettings, TitleSettings titleSettings, LabelSettings labelSettings, string defaultSelectedKey, Thickness listBoxMargin, Brush listBoxBackground, Brush buttonBackgrounds, Brush buttonBorders, Brush buttonOnMouseOverColors, bool applyDropShadow, Helpers.SetFocus setListBoxFocusType, int secondsToDelay, Action<ListBox> addItemsToCombo, Action<Object, SelectionChangedEventArgs> optionalSelectionChangedHandler, Action<string> OKClickAction, Action<string> CANCELClickAction, Action<string> COMMON_CLOSEClickAction)
        {
            Border bdr = ShowCustomFormDialog(canVas, dialogGuid, titleSettings.TitleCaption, width, internarlGridRowHeight, borderWidth, borderHeight, left, top, dialogBoxBorder, dialogBoxBorderThickness, borderBackground, titleSettings.TitleBackground, buttonBackgrounds, buttonBorders, buttonOnMouseOverColors, applyDropShadow,
                (sp, okButton, cancelButton, addContactLabel) =>
                {

                    Canvas innerCanvas = canVas;
                    addContactLabel.Height = titleSettings.TitleHeight;
                    addContactLabel.Margin = titleSettings.TitleMargin;
                    addContactLabel.Background = titleSettings.TitleBackground;
                    addContactLabel.Foreground = titleSettings.TitleForeground;
                    addContactLabel.SetValue(TextOptions.TextFormattingModeProperty, TextFormattingMode.Display);

                    //Font Settings 1 and 2                                                                                             
                    FasterWPF.FontExt.ApplyFontSettings(titleSettings.TitleFontSettings, addContactLabel);

                    //Grid Settings                                   
                    var glGr = new List<GridUnitType>();
                    glGr.Add(GridUnitType.Pixel);
                    var rlRh = new List<double>();
                    rlRh.Add(internarlGridRowHeight - 5);
                    var wlRh = new List<double>();
                    wlRh.Add(width - 10);
                    var newGridSettings = new FasterWPF.GridSettings();
                    newGridSettings.NumColumns = 1;
                    newGridSettings.NumRows = 1;
                    newGridSettings.ColUnitTypes = glGr;
                    newGridSettings.RowHeights = rlRh;
                    newGridSettings.RowUnitTypes = glGr;
                    newGridSettings.ColWidths = wlRh;
                    newGridSettings.Height = internarlGridRowHeight - 5;
                    newGridSettings.Width = borderWidth;
                    var rlRhLst = new List<double>();
                    rlRhLst.Add(listBoxRowHeight - 5);
                    var newGridSettingsLst = new FasterWPF.GridSettings();
                    newGridSettingsLst.NumColumns = 1;
                    newGridSettingsLst.NumRows = 1;
                    newGridSettingsLst.ColUnitTypes = glGr;
                    newGridSettingsLst.RowHeights = rlRhLst;
                    newGridSettingsLst.RowUnitTypes = glGr;
                    newGridSettingsLst.ColWidths = wlRh;
                    newGridSettingsLst.Height = listBoxSettings.Height - 5;
                    newGridSettingsLst.Width = borderWidth;
                    
                    labelSettings.LabelMinWidth = labelSettings.LabelMinWidth - 10;
                    labelSettings.LabelHeight = null; //restrict this for now . . .

                    var myListBox = sp.CreateLabelAndListBox(listBoxSettings, labelSettings, internarlGridRowHeight - 5, listBoxMargin, newGridSettings, newGridSettingsLst, listBoxBackground, 1, optionalSelectionChangedHandler);
                    addItemsToCombo(myListBox);

                    myListBox.SetSelectedContentControlColor( BrushExt.CreateColorFromString(listBoxSettings.HighlightBackgroundColor) );
                    myListBox.SetSelectedContentControlForeground( BrushExt.CreateSolidColorBrushFromString(listBoxSettings.HighlightForegroundColor) ); 

                    //Increase the height of the StackPanel and surrounding Border to make room for extra fields
                    sp.Height = 730.0;
                    (sp.GetBorderFromChildElement<StackPanel>() as Border).Height = 730.0;

                    Action<int> commonOKClickAction = o =>
                    {
                        Canvas secondCanvas = innerCanvas;
                        string selectedText = myListBox.SelectedContentControl().GetTextRuns<Grid, ListBox>().FirstOrDefault();

                        OKClickAction(selectedText.Trim());

                        //CLOSE DIALOG
                        if (FasterWPF.CommonExt.IsCompositeOnParent<Canvas>(secondCanvas, dialogGuid))
                        {
                            FasterWPF.Selectors.Select.DisposeEventsOnAllParents(sp);  //this will unsubscribe the SelectionChanged Event
                            FasterWPF.CommonExt.RemoveByKey<Canvas>(secondCanvas, dialogGuid);
                            COMMON_CLOSEClickAction(selectedText.Trim());
                        }
                    };
                    Action<Object, MouseButtonEventArgs> okClickHandlerMouse = ((Object o1, MouseButtonEventArgs ev1) =>
                    {
                        commonOKClickAction(0);
                    });

                    //Remove existing subscriptions for OK button and hook up new Click event             
                    FasterWPF.CommonExt.UnsubscribeMouseSingleClickOnBorderAll<Border>(okButton, true);
                    FasterWPF.CommonExt.SubscribeEventOnParent<FrameworkElement, MouseButtonEventArgs>(okButton, "PreviewMouseDown", okClickHandlerMouse);

                    //Remove existing subscriptions for CANCEL button and hook up new Click event 
                    Action<Object, MouseButtonEventArgs> mbehCancel = (o1, ev) =>
                    {
                        Canvas thirdCanvas = innerCanvas;
                        //CLOSE DIALOG
                        if (FasterWPF.CommonExt.IsCompositeOnParent<Canvas>(thirdCanvas, dialogGuid))
                        {
                            FasterWPF.Selectors.Select.DisposeEventsOnAllParents(sp);  //this will unsubscribe the SelectionChanged Event
                            FasterWPF.CommonExt.RemoveByKey<Canvas>(thirdCanvas, dialogGuid);

                            string selectedText = myListBox.SelectedContentControl().GetTextRuns<Grid, ListBox>().FirstOrDefault();
                            CANCELClickAction(selectedText.Trim());
                            COMMON_CLOSEClickAction(selectedText.Trim());
                        }
                    };
                    FasterWPF.CommonExt.UnsubscribeMouseSingleClickOnBorderAll<Border>(cancelButton, true);
                    FasterWPF.CommonExt.SubscribeEventOnParent<FrameworkElement, MouseButtonEventArgs>(cancelButton, "PreviewMouseDown", mbehCancel);

                    //Capture ESC and ENTER Key Input
                    Action<Object, KeyEventArgs> keyInputs = (o1, ev1) =>
                    {
                        Canvas fourCanvas = innerCanvas;

                        //CLOSE DIALOG
                        if (ev1.Key == Key.Escape && (FasterWPF.CommonExt.IsCompositeOnParent<Canvas>(fourCanvas, dialogGuid)))
                        {
                            FasterWPF.Selectors.Select.DisposeEventsOnAllParents(sp);  //this will unsubscribe the SelectionChanged Event
                            FasterWPF.CommonExt.RemoveByKey<Canvas>(fourCanvas, dialogGuid);

                            string selectedText = myListBox.SelectedContentControl().GetTextRuns<Grid, ListBox>().FirstOrDefault();
                            CANCELClickAction(selectedText.Trim());
                            COMMON_CLOSEClickAction(selectedText.Trim());
                        }
                        if (ev1.Key == Key.Enter)
                        {
                            commonOKClickAction(0);
                        }
                    };
                    FasterWPF.CommonExt.SubscribeEventOnParent<FrameworkElement, KeyEventArgs>(sp, "PreviewKeyDown", keyInputs);

                    if (defaultSelectedKey != null)
                    {
                        myListBox.SetSelectedKey(defaultSelectedKey);

                        //myComboBox.SetSelectedKey(cbGuid3);  if you need to set by GUID key, you could likely use this . . .
                    }
                }
                                                   ); //end of ShowCustomFormDialog buildFormAction Function       

            //set focus to last field and then tab to first field
           if (bdr.IsThisElementAParent())
           {
                List<ListBox> ltBox = FasterWPF.Selectors.Select.SelectFrameworkElementsByType<ListBox>(bdr);
                if (ltBox != null && ltBox.Count > 0)
                {
                    var theTextBox = ltBox.Last();

                    if (setListBoxFocusType == Helpers.SetFocus.Delayed)
                    {
                        theTextBox.WaitForXSeconds(secondsToDelay, () =>
                        {
                            System.Windows.Forms.SendKeys.SendWait("{Tab}");
                            Keyboard.Focus(theTextBox);
                            theTextBox.Focus();
                        });
                    }

                    if (setListBoxFocusType == Helpers.SetFocus.Immediate)
                    {
                        System.Windows.Forms.SendKeys.SendWait("{Tab}");
                        Keyboard.Focus(theTextBox);
                        theTextBox.Focus();
                    }
                }
            }

            return bdr;
        }

        /// <summary>
        /// Display a ComboBox Dialog. Will NOT display if another dialog with the SAME dialogGuid is already displaying. buildFormAction contains StackPanel for body, okButton Border, cancelButton Border, and title Label.
        /// </summary>
        /// <param name="canVas"></param>
        /// <param name="dialogGuid"></param>
        /// <param name="width"></param>
        /// <param name="internarlGridRowHeight"></param>
        /// <param name="borderWidth"></param>
        /// <param name="borderHeight"></param>
        /// <param name="left"></param>
        /// <param name="top"></param>
        /// <param name="dialogBoxBorder"></param>
        /// <param name="dialogBoxBorderThickness"></param>
        /// <param name="borderBackground"></param>
        /// <param name="comboBoxSettings"></param>
        /// <param name="titleSettings"></param>
        /// <param name="labelSettings"></param>
        /// <param name="questionDefaultText"></param>
        /// <param name="comboBoxMargin"></param>
        /// <param name="comboBoxBackground"></param>
        /// <param name="comboBoxMaxLength"></param>
        /// <param name="buttonBackgrounds"></param>
        /// <param name="buttonBorders"></param>
        /// <param name="buttonOnMouseOverColors"></param>
        /// <param name="setQuestionBoxFocusType"></param>
        /// <param name="secondsToDelay"></param>
        /// <param name="OKClickAction"></param>
        /// <param name="CANCELClickAction"></param>
        /// <param name="COMMON_CLOSEClickAction"></param>
        /// <returns></returns>
        public static Border ShowComboBoxDialog(Canvas canVas, string dialogGuid, double width, double internarlGridRowHeight, double borderWidth, double borderHeight, double left, double top, Brush dialogBoxBorder, Thickness dialogBoxBorderThickness, Brush borderBackground, ComboBoxSettings comboBoxSettings, TitleSettings titleSettings, LabelSettings labelSettings, string defaultSelection, Thickness comboBoxMargin, Brush comboBoxBackground, Brush buttonBackgrounds, Brush buttonBorders, Brush buttonOnMouseOverColors, bool applyDropShadow, Helpers.SetFocus setComboBoxFocusType, int secondsToDelay, Action<ComboBox> addItemsToCombo, Action<Object, SelectionChangedEventArgs> optionalSelectionChangedHandler,  Action<string> OKClickAction, Action<string> CANCELClickAction, Action<string> COMMON_CLOSEClickAction)
        {
            Border bdr = ShowCustomFormDialog(canVas, dialogGuid, titleSettings.TitleCaption, width, internarlGridRowHeight, borderWidth, borderHeight, left, top, dialogBoxBorder, dialogBoxBorderThickness, borderBackground, titleSettings.TitleBackground, buttonBackgrounds, buttonBorders, buttonOnMouseOverColors, applyDropShadow,
                (sp, okButton, cancelButton, addContactLabel) =>
                {

                    Canvas innerCanvas = canVas;
                    addContactLabel.Height = titleSettings.TitleHeight;
                    addContactLabel.Margin = titleSettings.TitleMargin;
                    addContactLabel.Background = titleSettings.TitleBackground;
                    addContactLabel.Foreground = titleSettings.TitleForeground;
                    addContactLabel.SetValue(TextOptions.TextFormattingModeProperty, TextFormattingMode.Display);

                    //Font Settings 1 and 2                                                                                             
                    FasterWPF.FontExt.ApplyFontSettings(titleSettings.TitleFontSettings, addContactLabel);

                    //Grid Settings                                   
                    var glGr = new List<GridUnitType>();
                    glGr.Add(GridUnitType.Pixel);
                    var rlRh = new List<double>();
                    rlRh.Add(internarlGridRowHeight - 5);
                    var wlRh = new List<double>();
                    wlRh.Add(width - 10);
                    var newGridSettings = new FasterWPF.GridSettings();
                    newGridSettings.NumColumns = 1;
                    newGridSettings.NumRows = 1;
                    newGridSettings.ColUnitTypes = glGr;
                    newGridSettings.RowHeights = rlRh;
                    newGridSettings.RowUnitTypes = glGr;
                    newGridSettings.ColWidths = wlRh;
                    newGridSettings.Height = internarlGridRowHeight - 5;
                    newGridSettings.Width = borderWidth;

                    labelSettings.LabelMinWidth = labelSettings.LabelMinWidth - 10;
                    labelSettings.LabelHeight = null; //restrict this for now . . .

                    var myComboBox = sp.CreateLabelAndComboBox(comboBoxSettings, labelSettings, internarlGridRowHeight - 5, comboBoxMargin, newGridSettings, comboBoxBackground, 1, optionalSelectionChangedHandler);
                    addItemsToCombo(myComboBox);

                    //Increase the height of the StackPanel and surrounding Border to make room for extra fields
                    sp.Height = 730.0;
                    (sp.GetBorderFromChildElement<StackPanel>() as Border).Height = 730.0;

                    Action<int> commonOKClickAction = o =>
                    {

                        Canvas secondCanvas = innerCanvas;
                        if (System.String.IsNullOrWhiteSpace(myComboBox.Text)) { myComboBox.Text = " "; };

                        OKClickAction(myComboBox.Text.Trim());

                        //CLOSE DIALOG
                        if (FasterWPF.CommonExt.IsCompositeOnParent<Canvas>(secondCanvas, dialogGuid))
                        {
                            FasterWPF.Selectors.Select.DisposeEventsOnAllParents(sp);  //this will unsubscribe the SelectionChanged Event
                            FasterWPF.CommonExt.RemoveByKey<Canvas>(secondCanvas, dialogGuid);
                            COMMON_CLOSEClickAction(myComboBox.Text.Trim());
                        }
                    };
                    Action<Object, MouseButtonEventArgs> okClickHandlerMouse = ((Object o1, MouseButtonEventArgs ev1) =>
                    {
                        commonOKClickAction(0);
                    });

                    //Remove existing subscriptions for OK button and hook up new Click event             
                    FasterWPF.CommonExt.UnsubscribeMouseSingleClickOnBorderAll<Border>(okButton, true);
                    FasterWPF.CommonExt.SubscribeEventOnParent<FrameworkElement, MouseButtonEventArgs>(okButton, "PreviewMouseDown", okClickHandlerMouse);

                    //Remove existing subscriptions for CANCEL button and hook up new Click event 
                    Action<Object, MouseButtonEventArgs> mbehCancel = (o1, ev) =>
                    {
                        Canvas thirdCanvas = innerCanvas;
                        //CLOSE DIALOG
                        if (FasterWPF.CommonExt.IsCompositeOnParent<Canvas>(thirdCanvas, dialogGuid))
                        {
                            FasterWPF.Selectors.Select.DisposeEventsOnAllParents(sp);  //this will unsubscribe the SelectionChanged Event
                            FasterWPF.CommonExt.RemoveByKey<Canvas>(thirdCanvas, dialogGuid);

                            CANCELClickAction(myComboBox.Text.Trim());
                            COMMON_CLOSEClickAction(myComboBox.Text.Trim());
                        }
                    };
                    FasterWPF.CommonExt.UnsubscribeMouseSingleClickOnBorderAll<Border>(cancelButton, true);
                    FasterWPF.CommonExt.SubscribeEventOnParent<FrameworkElement, MouseButtonEventArgs>(cancelButton, "PreviewMouseDown", mbehCancel);

                    //Capture ESC and ENTER Key Input
                    Action<Object, KeyEventArgs> keyInputs = (o1, ev1) =>
                    {
                        Canvas fourCanvas = innerCanvas;

                        //CLOSE DIALOG
                        if (ev1.Key == Key.Escape && (FasterWPF.CommonExt.IsCompositeOnParent<Canvas>(fourCanvas, dialogGuid)))
                        {
                            FasterWPF.Selectors.Select.DisposeEventsOnAllParents(sp);  //this will unsubscribe the SelectionChanged Event
                            FasterWPF.CommonExt.RemoveByKey<Canvas>(fourCanvas, dialogGuid);

                            CANCELClickAction(myComboBox.Text.Trim());
                            COMMON_CLOSEClickAction(myComboBox.Text.Trim());
                        }
                        if (ev1.Key == Key.Enter)
                        {
                            commonOKClickAction(0);
                        }
                    };
                    FasterWPF.CommonExt.SubscribeEventOnParent<FrameworkElement, KeyEventArgs>(sp, "PreviewKeyDown", keyInputs);

                    if (defaultSelection != null)
                    {
                        myComboBox.Text = defaultSelection;

                        //myComboBox.SetSelectedKey(cbGuid3);  if you need to set by GUID key, you could likely use this . . .
                    }
                }
                                                   ); //end of ShowCustomFormDialog buildFormAction Function       

            //set focus to last field and then tab to first field
            if (bdr.IsThisElementAParent())
            {
                List<ComboBox> ltBox = FasterWPF.Selectors.Select.SelectFrameworkElementsByType<ComboBox>(bdr);
                if (ltBox != null && ltBox.Count > 0)
                {
                    var theTextBox = ltBox.Last();

                    if (setComboBoxFocusType == Helpers.SetFocus.Delayed)
                    {
                        theTextBox.WaitForXSeconds(secondsToDelay, () =>
                        {
                            System.Windows.Forms.SendKeys.SendWait("{Tab}");
                            Keyboard.Focus(theTextBox);
                            theTextBox.Focus();
                        });
                    }

                    if (setComboBoxFocusType == Helpers.SetFocus.Immediate)
                    {
                        System.Windows.Forms.SendKeys.SendWait("{Tab}");
                        Keyboard.Focus(theTextBox);
                        theTextBox.Focus();
                    }
                }
            }
            return bdr;
        }

        /// <summary>
        /// Display a Question Box Dialog. Will NOT display if another dialog with the SAME dialogGuid is already displaying. buildFormAction contains StackPanel for body, okButton Border, cancelButton Border, and title Label.
        /// </summary>
        /// <param name="canVas"></param>
        /// <param name="dialogGuid"></param>
        /// <param name="labelCaption"></param>
        /// <param name="width"></param>
        /// <param name="internarlGridRowHeight"></param>
        /// <param name="borderWidth"></param>
        /// <param name="borderHeight"></param>
        /// <param name="left"></param>
        /// <param name="top"></param>
        /// <param name="dialogBoxBorder"></param>
        /// <param name="dialogBoxBorderThickness"></param>
        /// <param name="borderBackground"></param>
        /// <param name="labelBackground"></param>
        /// <param name="labelForeground"></param>
        /// <param name="labelMargin"></param>
        /// <param name="labelHeight"></param>
        /// <param name="labelFontSettings"></param>
        /// <param name="questionFontSettings"></param>
        /// <param name="questionText"></param>
        /// <param name="questionDefaultText"></param>
        /// <param name="questionTextMargin"></param>
        /// <param name="questionTextBoxMargin"></param>
        /// <param name="questionTextBackground"></param>
        /// <param name="questionTextBoxBackground"></param>
        /// <param name="buttonBackgrounds"></param>
        /// <param name="buttonBorders"></param>
        /// <param name="buttonOnMouseOverColors"></param>
        /// <param name="OKClickAction"></param>
        /// <param name="CANCELClickAction"></param>
        /// <param name="COMMON_CLOSEClickAction"></param>
        /// <returns></returns>
        public static Border ShowQuestionBoxDialog(Canvas canVas, string dialogGuid, double width, double internarlGridRowHeight, double borderWidth, double borderHeight, double left, double top, Brush dialogBoxBorder, Thickness dialogBoxBorderThickness, Brush borderBackground, TitleSettings titleSettings, LabelSettings labelSettings, string questionDefaultText, Thickness questionTextBoxMargin, Brush questionTextBoxBackground, int textBoxMaxLength, Brush buttonBackgrounds, Brush buttonBorders, Brush buttonOnMouseOverColors, bool applyDropShadow, Helpers.SetFocus setQuestionBoxFocusType, int secondsToDelay, Action<string> OKClickAction, Action<string> CANCELClickAction, Action<string> COMMON_CLOSEClickAction)
        {
            Border bdr = ShowCustomFormDialog(canVas, dialogGuid, titleSettings.TitleCaption, width, internarlGridRowHeight, borderWidth, borderHeight, left, top, dialogBoxBorder, dialogBoxBorderThickness, borderBackground, titleSettings.TitleBackground, buttonBackgrounds, buttonBorders, buttonOnMouseOverColors, applyDropShadow,           
                (sp, okButton, cancelButton, addContactLabel)=>{

                                                               Canvas innerCanvas=canVas;
                                                               addContactLabel.Height=titleSettings.TitleHeight;
                                                               addContactLabel.Margin=titleSettings.TitleMargin;
                                                               addContactLabel.Background=titleSettings.TitleBackground;
                                                               addContactLabel.Foreground=titleSettings.TitleForeground;
                                                               addContactLabel.SetValue(TextOptions.TextFormattingModeProperty, TextFormattingMode.Display);

                                                                //Font Settings 1 and 2                                                                                             
                                                               FasterWPF.FontExt.ApplyFontSettings(titleSettings.TitleFontSettings, addContactLabel);
                                                                                         
                                                               //Grid Settings                                   
                                                               var glGr=new List<GridUnitType>();
                                                               glGr.Add(GridUnitType.Pixel);
                                                               var rlRh=new List<double>();
                                                               rlRh.Add( internarlGridRowHeight-5);
                                                               var wlRh=new List<double>();
                                                               wlRh.Add(width-10);                      
                                                               var newGridSettings=new FasterWPF.GridSettings();
                                                               newGridSettings.NumColumns=1;
                                                               newGridSettings.NumRows=1;
                                                               newGridSettings.ColUnitTypes=glGr;
                                                               newGridSettings.RowHeights=rlRh;
                                                               newGridSettings.RowUnitTypes=glGr;
                                                               newGridSettings.ColWidths=wlRh;
                                                               newGridSettings.Height =  internarlGridRowHeight-5;
                                                               newGridSettings.Width = borderWidth;

                                                               labelSettings.LabelMinWidth = labelSettings.LabelMinWidth- 10;
                                                               labelSettings.LabelHeight = null; //restrict this for now . . .

                                                               var myTextBox = sp.CreateLabelAndTextBox(labelSettings, internarlGridRowHeight - 5, textBoxMaxLength, questionTextBoxMargin, newGridSettings, labelSettings.LabelMinWidth, questionTextBoxBackground, 1);

                                                               //Increase the height of the StackPanel and surrounding Border to make room for extra fields
                                                               sp.Height=730.0;
                                                               (sp.GetBorderFromChildElement<StackPanel>() as Border).Height=730.0;

                                                               Action<int> commonOKClickAction = o=>{
                                                                                  
                                                                                     Canvas secondCanvas = innerCanvas;
                                                                                     if (System.String.IsNullOrWhiteSpace(myTextBox.Text)) { myTextBox.Text = " "; }; 

                                                                                      OKClickAction(myTextBox.Text.Trim());                                                                                                              
                                                                                        
                                                                                     //CLOSE DIALOG
                                                                                     if (FasterWPF.CommonExt.IsCompositeOnParent<Canvas>(secondCanvas, dialogGuid))
                                                                                     {
                                                                                         FasterWPF.Selectors.Select.DisposeEventsOnAllParents(sp);
                                                                                          FasterWPF.CommonExt.RemoveByKey<Canvas>(secondCanvas, dialogGuid);
                                                                                          COMMON_CLOSEClickAction(myTextBox.Text.Trim());
                                                                                     }
                                                               };   
                                                               Action<Object, MouseButtonEventArgs> okClickHandlerMouse = ((Object o1, MouseButtonEventArgs ev1)=>
                                                               {
                                                                                   commonOKClickAction(0);
                                                               });
                                                              
                                                               //Remove existing subscriptions for OK button and hook up new Click event             
                                                               FasterWPF.CommonExt.UnsubscribeMouseSingleClickOnBorderAll<Border>(okButton, true);
                                                               FasterWPF.CommonExt.SubscribeEventOnParent<FrameworkElement, MouseButtonEventArgs>(okButton, "PreviewMouseDown", okClickHandlerMouse);

                                                               //Remove existing subscriptions for CANCEL button and hook up new Click event 
                                                               Action<Object, MouseButtonEventArgs> mbehCancel = (o1, ev)=>{
                                                                          Canvas thirdCanvas = innerCanvas;
                                                                          //CLOSE DIALOG
                                                                          if (FasterWPF.CommonExt.IsCompositeOnParent<Canvas>(thirdCanvas, dialogGuid))
                                                                          {
                                                                              FasterWPF.Selectors.Select.DisposeEventsOnAllParents(sp);
                                                                                  FasterWPF.CommonExt.RemoveByKey<Canvas>(thirdCanvas, dialogGuid);
                                                                            
                                                                                  CANCELClickAction(myTextBox.Text.Trim());
                                                                                  COMMON_CLOSEClickAction(myTextBox.Text.Trim());
                                                                          }
                                                               };
                                                               FasterWPF.CommonExt.UnsubscribeMouseSingleClickOnBorderAll<Border>(cancelButton, true);
                                                               FasterWPF.CommonExt.SubscribeEventOnParent<FrameworkElement, MouseButtonEventArgs>(cancelButton, "PreviewMouseDown", mbehCancel);

                                                               //Capture ESC and ENTER Key Input
                                                               Action<Object, KeyEventArgs> keyInputs = (o1, ev1)=>{
                                                                         Canvas fourCanvas = innerCanvas;
                                                                    
                                                                          //CLOSE DIALOG
                                                                          if (ev1.Key == Key.Escape && (FasterWPF.CommonExt.IsCompositeOnParent<Canvas>(fourCanvas, dialogGuid)))
                                                                          {
                                                                                  FasterWPF.Selectors.Select.DisposeEventsOnAllParents(sp);
                                                                                  FasterWPF.CommonExt.RemoveByKey<Canvas>(fourCanvas, dialogGuid);
                                                                      
                                                                                  CANCELClickAction(myTextBox.Text.Trim());
                                                                                  COMMON_CLOSEClickAction(myTextBox.Text.Trim());
                                                                          }
                                                                          if( ev1.Key == Key.Enter )
                                                                          {
                                                                                  commonOKClickAction(0);
                                                                          }
                                                               };
                                                               FasterWPF.CommonExt.SubscribeEventOnParent<FrameworkElement, KeyEventArgs>(sp, "PreviewKeyDown", keyInputs);
                                                               
                                                               if (questionDefaultText!=null)
                                                               {                                             
                                                                     myTextBox.Text=questionDefaultText;             
                                                               }
                                                            }
                                                   ); //end of ShowCustomFormDialog buildFormAction Function       
       
            //set focus to last field and then tab to first field
            if (bdr.IsThisElementAParent())
            {
                List<TextBox> ltBox = FasterWPF.Selectors.Select.SelectFrameworkElementsByType<TextBox>(bdr);
                if (ltBox != null && ltBox.Count > 0)
                {
                    var theTextBox = ltBox.Last();

                    if (setQuestionBoxFocusType == Helpers.SetFocus.Delayed)
                    {
                        theTextBox.WaitForXSeconds(secondsToDelay, () =>
                        {
                            System.Windows.Forms.SendKeys.SendWait("{Tab}");
                            Keyboard.Focus(theTextBox);
                            theTextBox.Focus();
                        });
                    }

                    if (setQuestionBoxFocusType == Helpers.SetFocus.Immediate)
                    {
                        System.Windows.Forms.SendKeys.SendWait("{Tab}");
                        Keyboard.Focus(theTextBox);
                        theTextBox.Focus();
                    }
                }
            }
            return bdr;
        }

        /// <summary>
        /// Display a Custom Form Dialog. Will NOT display if another dialog with the SAME dialogGuid is already displaying. buildFormAction contains StackPanel for body, okButton Border, cancelButton Border, and title Label.
        /// </summary>
        /// <param name="canVas"></param>
        /// <param name="dialogGuid"></param>
        /// <param name="caption"></param>
        /// <param name="width"></param>
        /// <param name="internalGridRowHeight"></param>
        /// <param name="borderWidth"></param>
        /// <param name="borderHeight"></param>
        /// <param name="left"></param>
        /// <param name="top"></param>
        /// <param name="dialogBoxBorder"></param>
        /// <param name="dialogBoxBorderThickness"></param>
        /// <param name="borderBackground"></param>
        /// <param name="labelBackground"></param>
        /// <param name="buttonBackgrounds"></param>
        /// <param name="buttonBorders"></param>
        /// <param name="buttonOnMouseOverColors"></param>
        /// <param name="buildFormAction"></param>
        /// <returns></returns>
        public static Border ShowCustomFormDialog(Canvas canVas, string dialogGuid, string caption, double width, double internalGridRowHeight, double borderWidth, double borderHeight, double left, double top, Brush dialogBoxBorder, Thickness dialogBoxBorderThickness, Brush borderBackground, Brush labelBackground, Brush buttonBackgrounds, Brush buttonBorders, Brush buttonOnMouseOverColors, bool applyDropShadow, Action<StackPanel, Border, Border, Label> buildFormAction)
        {
            double height = 149D;
            Border brd1 = new Border();
            brd1.Initialize(width, height, ContainerType.HorizontalPanel, dialogBoxBorder, dialogBoxBorderThickness, Brushes.WhiteSmoke);
            brd1.BeginSettings<Border>().SetItemBorderSettings<Border>(width, height, Brushes.Transparent, new Thickness(0)).EndSettings<Border>();

            if (applyDropShadow)
            {
                brd1.Effect = new DropShadowEffect();
            }               

            if (!canVas.IsCompositeOnParent<Canvas>(dialogGuid))
            {
                canVas.BeginComposite(dialogGuid)
                   .AddExisting<Border, Canvas>(0, 0, brd1)
                   .EndComposite<Canvas, CanvasArgs>(new CanvasArgs(left, top, 500));

                //StackPanel
                StackPanel stkPanel1 = new StackPanel();
                stkPanel1.SetBackgroundColor(borderBackground);
                stkPanel1.Initialize(width, height, ContainerType.Grid, Orientation.Vertical);
                stkPanel1.SetCompositeGridDimensionsWithRowHt(1, 1, internalGridRowHeight, GridUnitType.Pixel, width, GridUnitType.Pixel);      
                                
                brd1.BeginComposite()
                  .AddExisting<StackPanel, Border>(0, 0, stkPanel1)
                  .EndComposite<Border, BorderArgs>(null);

                stkPanel1.BeginSettings<StackPanel>()
                    .SetFontOnLabel<StackPanel>(0, 0, "Calibri", 13, FontWeights.Normal, FontStyles.Normal)
                    .Set<Border, StackPanel>(0, 2, "Margin", new Thickness(0, 0, 10, 7))
                    .Set<Border, StackPanel>(0, 1, "HorizontalAlignment", HorizontalAlignment.Right)
                    .Set<Border, StackPanel>(0, 2, "HorizontalAlignment", HorizontalAlignment.Right)
                    .EndSettings<StackPanel>();

                stkPanel1.BeginComposite()
                    .AddFlatButton<StackPanel>(0, 1, 21D, 21D, " X", buttonBorders, new Thickness(1), buttonBackgrounds, new CornerRadius(1), new Thickness(2, 1, 1, 1), new Thickness(0, 0, 10, 7), buttonOnMouseOverColors, (o1, e) =>
                    { //CLOSE DIALOG
                        if (canVas.IsCompositeOnParent<Canvas>(dialogGuid))
                        {
                            FasterWPF.Selectors.Select.DisposeEventsOnAllParents(stkPanel1);
                            canVas.RemoveByKey<Canvas>(dialogGuid);
                        }
                    })
                   .EndComposite<StackPanel, StackPanelArgs>(null);

                stkPanel1.BeginComposite()
                    .AddLabel<StackPanel>(0, 0, caption, labelBackground)
                    .EndComposite<StackPanel, StackPanelArgs>(null);

                Border borderButton3 = new Border();
                borderButton3.MaxHeight = 30;
                borderButton3.Initialize(double.NaN, double.NaN, ContainerType.HorizontalPanel, buttonBorders, new Thickness(2), buttonBackgrounds, new CornerRadius(10), new Thickness(0), new Thickness(1));

                borderButton3.BeginSettings<Border>()
                    .SetTextPadding<Border>(0, 0, new Thickness(4))
                    .SubscribeMouseSingleClickOnBorder<Border>((o1, ev1) =>
                    {   //CLOSE DIALOG
                        if (canVas.IsCompositeOnParent<Canvas>(dialogGuid))
                        {
                            FasterWPF.Selectors.Select.DisposeEventsOnAllParents(stkPanel1);
                            canVas.RemoveByKey<Canvas>(dialogGuid);
                        }
                    })
                    .EndSettings<Border>();

                borderButton3.SetMouseOverColorOnBorder(buttonOnMouseOverColors);

                borderButton3.BeginComposite()
                    .AddText<Border>(0, 0, "  OK  ")
                    .EndComposite<Border, BorderArgs>(null);

                stkPanel1.BeginComposite()
                         .AddExisting<Border, StackPanel>(0, 2, borderButton3)
                         .EndComposite<StackPanel, StackPanelArgs>(null);

                brd1.Width = borderWidth;
                brd1.Height = borderHeight;

                var act1 = new Action<FrameworkElement>((FrameworkElement fe) =>
                    {
                        if (fe is StackPanel)
                        {
                            SettingsManager.BeginSettings((StackPanel)fe).Set<Label, StackPanel>("Width", 50.0).Set<Label, StackPanel>("Height", 50.0).EndSettings();

                            //To insert some new elements we have to re-arrange the OK Button by removing and then re-adding it further below
                            var cancelButton = (Border)FasterWPF.CommonExt.GetChildLookupDictionary<StackPanel>((StackPanel)fe).ElementAt(0).Value;
                            var okButton = (Border)FasterWPF.CommonExt.GetChildLookupDictionary<StackPanel>((StackPanel)fe).ElementAt(2).Value;
                            string myKey = FasterWPF.CommonExt.GetKey((Border)okButton.GetBorderFromChildElement<Border>());
                            FasterWPF.CommonExt.RemoveByKey((StackPanel)fe, myKey);

                            var theLabel = (Label)FasterWPF.CommonExt.GetChildLookupDictionary<StackPanel>((StackPanel)fe).ElementAt(1).Value;
                            buildFormAction(stkPanel1, okButton, cancelButton, theLabel);
                             //Add OK button back                                                            
                            FasterWPF.CommonExt.BeginComposite<StackPanel>(stkPanel1).AddExisting<Border, StackPanel>(0, 2, okButton).EndComposite<StackPanel, StackPanelArgs>(null);
                        }
                    });

                brd1.ActOnParentFrameworkElements(act1);

            }

            return brd1;
        }

        /// <summary>
        /// Display an OK Dialog. Will NOT display if another dialog with the SAME dialogGuid is already displaying.
        /// </summary>
        /// <param name="canVas"></param>
        /// <param name="dialogGuid"></param>
        /// <param name="caption"></param>
        /// <param name="width"></param>
        /// <param name="left"></param>
        /// <param name="top"></param>
        /// <param name="dialogBoxBorder"></param>
        /// <param name="dialogBoxBorderThickness"></param>
        /// <param name="borderBackground"></param>
        /// <param name="labelBackground"></param>
        /// <param name="buttonBackgrounds"></param>
        /// <param name="buttonBorders"></param>
        /// <param name="buttonOnMouseOverColors"></param>
        /// <returns></returns>
        public static Border  ShowOKDialog(Canvas canVas, string dialogGuid, string caption, double width, double left, double top, Brush dialogBoxBorder, Thickness dialogBoxBorderThickness, Brush borderBackground, Brush labelBackground, Brush buttonBackgrounds, Brush buttonBorders, Brush buttonOnMouseOverColors, bool applyDropShadow)
        {
            double height = 149D;
            Border brd1 = new Border();
            brd1.Initialize(width, height, ContainerType.HorizontalPanel, dialogBoxBorder, dialogBoxBorderThickness, Brushes.WhiteSmoke);
            brd1.BeginSettings<Border>().SetItemBorderSettings<Border>(width, height, Brushes.Transparent, new Thickness(0)).EndSettings<Border>();

            if (applyDropShadow)
            {
                brd1.Effect = new DropShadowEffect();
            }

            if (!canVas.IsCompositeOnParent<Canvas>(dialogGuid))
            {
                canVas.BeginComposite(dialogGuid)
                    .AddExisting<Border, Canvas>(0, 0, brd1)
                    .EndComposite<Canvas, CanvasArgs>(new CanvasArgs(left, top, 500));

                //StackPanel
                StackPanel stkPanel1 = new StackPanel();
                stkPanel1.SetBackgroundColor(borderBackground);
                stkPanel1.Initialize(width, height, ContainerType.Grid, Orientation.Vertical);
                stkPanel1.SetCompositeGridDimensionsWithRowHt(1, 1, 50D, GridUnitType.Pixel, width, GridUnitType.Pixel);

                brd1.BeginComposite()
                  .AddExisting<StackPanel, Border>(0, 0, stkPanel1)
                  .EndComposite<Border, BorderArgs>(null);

                stkPanel1.BeginSettings<StackPanel>()
                    .SetFontOnLabel<StackPanel>(0, 0, "Calibri", 13, FontWeights.Normal, FontStyles.Normal)
                    .Set<Border, StackPanel>(0, 2, "Margin", new Thickness(0, 0, 10, 7))
                    .Set<Border, StackPanel>(0, 1, "HorizontalAlignment", HorizontalAlignment.Right)
                    .Set<Border, StackPanel>(0, 2, "HorizontalAlignment", HorizontalAlignment.Right)
                    .EndSettings<StackPanel>();

                stkPanel1.BeginComposite()
                    .AddAnything<TextBox, StackPanel>(0, 0).ModifyAtXY<TextBox, StackPanel>(0, 0, txtBox =>
                    {
                         txtBox.Height = 1.0D;
                    })
                    .AddFlatButton<StackPanel>(0, 1, 21D, 21D, " X", buttonBorders, new Thickness(1), buttonBackgrounds, new CornerRadius(1), new Thickness(2, 1, 1, 1), new Thickness(0, 0, 10, 7), buttonOnMouseOverColors, (o1, e) =>
                    { //CLOSE DIALOG
                        if (canVas.IsCompositeOnParent<Canvas>(dialogGuid))
                        {
                            FasterWPF.Selectors.Select.DisposeEventsOnAllParents(stkPanel1);
                            canVas.RemoveByKey<Canvas>(dialogGuid);
                        }
                    })
                   .EndComposite<StackPanel, StackPanelArgs>(null);

                stkPanel1.BeginComposite()
                    .AddLabel<StackPanel>(0, 0, caption, labelBackground)
                    .EndComposite<StackPanel, StackPanelArgs>(null);

                Border borderButton3 = new Border();
                borderButton3.MaxHeight = 30;
                borderButton3.Initialize(double.NaN, double.NaN, ContainerType.HorizontalPanel, buttonBorders, new Thickness(2), buttonBackgrounds, new CornerRadius(10), new Thickness(0), new Thickness(1));

                borderButton3.BeginSettings<Border>()
                    .SetTextPadding<Border>(0, 0, new Thickness(4))
                    .SubscribeMouseSingleClickOnBorder<Border>((o1, ev1) =>
                    {   //CLOSE DIALOG
                        if (canVas.IsCompositeOnParent<Canvas>(dialogGuid))
                        {
                            FasterWPF.Selectors.Select.DisposeEventsOnAllParents(stkPanel1);
                            canVas.RemoveByKey<Canvas>(dialogGuid);
                        }
                    })
                    .EndSettings<Border>();

                borderButton3.SetMouseOverColorOnBorder(buttonOnMouseOverColors);

                borderButton3.BeginComposite()
                    .AddText<Border>(0, 0, "  OK  ")
                    .EndComposite<Border, BorderArgs>(null);

                stkPanel1.BeginComposite()
                         .AddExisting<Border, StackPanel>(0, 2, borderButton3)
                         .EndComposite<StackPanel, StackPanelArgs>(null);

                //Capture ESC Key Input and Close Dialog
                Action<EventPattern<KeyEventArgs>> keyInputs = (eventPattern) =>
                {
                    //CLOSE DIALOG
                    if (eventPattern.EventArgs.Key == Key.Escape || eventPattern.EventArgs.Key == Key.Enter)
                    {
                        if (canVas.IsCompositeOnParent<Canvas>(dialogGuid))
                        {
                            FasterWPF.Selectors.Select.DisposeEventsOnAllParents(stkPanel1);
                            canVas.RemoveByKey<Canvas>(dialogGuid);
                        }
                    }
                };

                FasterWPF.CommonExt.SubscribeEventOnParent<StackPanel, KeyEventArgs>(stkPanel1, "PreviewKeyDown", keyInputs);

                var txtBox1 = FasterWPF.Selectors.Select.SelectFrameworkElementsByType<TextBox>(stkPanel1).First();

                System.Windows.Forms.SendKeys.SendWait("{Tab}");
                Keyboard.Focus(txtBox1);
                txtBox1.Focus();

            }

            return brd1;
        }

        /// <summary>
        /// Display a YES-NO-CANCEL Dialog. Will NOT display if another dialog with the SAME dialogGuid is already displaying.
        /// </summary>
        /// <param name="canVas"></param>
        /// <param name="dialogGuid"></param>
        /// <param name="caption"></param>
        /// <param name="width"></param>
        /// <param name="left"></param>
        /// <param name="top"></param>
        /// <param name="dialogBoxBorder"></param>
        /// <param name="dialogBoxBorderThickness"></param>
        /// <param name="borderBackground"></param>
        /// <param name="labelBackground"></param>
        /// <param name="buttonBackgrounds"></param>
        /// <param name="buttonBorders"></param>
        /// <param name="buttonOnMouseOverColors"></param>
        /// <param name="resultCallback"></param>
        /// <returns></returns>
        public static Border ShowYesNoCancelDialog(Canvas canVas, string dialogGuid, string caption, double width, double left, double top, Brush dialogBoxBorder, Thickness dialogBoxBorderThickness, Brush borderBackground, Brush labelBackground, Brush buttonBackgrounds, Brush buttonBorders, Brush buttonOnMouseOverColors, bool applyDropShadow, Action<bool> resultCallback)
        {
            double height = 149D;
            Border brd1 = new Border();
            brd1.Initialize(width, height, ContainerType.HorizontalPanel, dialogBoxBorder, dialogBoxBorderThickness, Brushes.WhiteSmoke);
            brd1.BeginSettings<Border>().SetItemBorderSettings<Border>(width, height, Brushes.Transparent, new Thickness(0)).EndSettings<Border>();

            if (applyDropShadow)
            {
                brd1.Effect = new DropShadowEffect();
            }

            if (!canVas.IsCompositeOnParent<Canvas>(dialogGuid))
            {
                canVas.BeginComposite(dialogGuid)
                    .AddExisting<Border, Canvas>(0, 0, brd1)
                    .EndComposite<Canvas, CanvasArgs>(new CanvasArgs(left, top, 500));

                //StackPanel
                StackPanel stkPanel1 = new StackPanel();
                stkPanel1.SetBackgroundColor(borderBackground);
                stkPanel1.Initialize(width, height, ContainerType.Grid, Orientation.Vertical);
                stkPanel1.SetCompositeGridDimensionsWithRowHt(1, 1, 50D, GridUnitType.Pixel, width, GridUnitType.Pixel);

                brd1.BeginComposite()
                  .AddExisting<StackPanel, Border>(0, 0, stkPanel1)
                  .EndComposite<Border, BorderArgs>(null);

                stkPanel1.BeginSettings<StackPanel>()
                    .SetFontOnLabel<StackPanel>(0, 0, "Calibri", 13, FontWeights.Normal, FontStyles.Normal)
                    .Set<Border, StackPanel>(0, 2, "Margin", new Thickness(0, 0, 10, 7))
                    .Set<Border, StackPanel>(0, 1, "HorizontalAlignment", HorizontalAlignment.Right)
                    .Set<Border, StackPanel>(0, 2, "HorizontalAlignment", HorizontalAlignment.Right)
                    .EndSettings<StackPanel>();

                stkPanel1.BeginComposite()
                    .AddAnything<TextBox, StackPanel>(0, 0).ModifyAtXY<TextBox, StackPanel>(0, 0, txtBox =>
                    {
                        txtBox.Height = 1.0D;
                    })
                    .AddFlatButton<StackPanel>(0, 1, 21D, 21D, " X", buttonBorders, new Thickness(1), buttonBackgrounds, new CornerRadius(1), new Thickness(2, 1, 1, 1), new Thickness(0, 0, 10, 7), buttonOnMouseOverColors, (o1, e) =>
                    { //CLOSE DIALOG
                        if (canVas.IsCompositeOnParent<Canvas>(dialogGuid))
                        {
                            FasterWPF.Selectors.Select.DisposeEventsOnAllParents(stkPanel1);
                            canVas.RemoveByKey<Canvas>(dialogGuid);
                            resultCallback(false);
                        }
                    })
                   .EndComposite<StackPanel, StackPanelArgs>(null);

                stkPanel1.BeginComposite()
                    .AddLabel<StackPanel>(0, 0, caption, labelBackground)
                    .EndComposite<StackPanel, StackPanelArgs>(null);

                //OK BUTTON
                Border borderButton3 = new Border();
                borderButton3.MaxHeight = 30;
                borderButton3.Margin = new Thickness(0.0, 0.0, 3.0, 0.0);
                borderButton3.Initialize(double.NaN, double.NaN, ContainerType.HorizontalPanel, buttonBorders, new Thickness(2), buttonBackgrounds, new CornerRadius(10), new Thickness(0), new Thickness(1));

                borderButton3.BeginSettings<Border>()
                    .SetTextPadding<Border>(0, 0, new Thickness(4))
                    .SubscribeMouseSingleClickOnBorder<Border>((o1, ev1) =>
                    {   //CLOSE DIALOG
                        if (canVas.IsCompositeOnParent<Canvas>(dialogGuid))
                        {
                            FasterWPF.Selectors.Select.DisposeEventsOnAllParents(stkPanel1);
                            canVas.RemoveByKey<Canvas>(dialogGuid);
                            resultCallback(true);
                        }
                    })
                    .EndSettings<Border>();

                borderButton3.SetMouseOverColorOnBorder(buttonOnMouseOverColors);

                borderButton3.BeginComposite()
                    .AddText<Border>(0, 0, "  Yes  ")
                    .EndComposite<Border, BorderArgs>(null);
            
                //CANCEL BUTTON
                Border borderButton5 = new Border();
                borderButton5.MaxHeight = 30;
                borderButton5.Margin = new Thickness(0.0, 0.0, 5.0, 0.0);
                borderButton5.Initialize(double.NaN, double.NaN, ContainerType.HorizontalPanel, buttonBorders, new Thickness(2), buttonBackgrounds, new CornerRadius(10), new Thickness(0), new Thickness(1));

                borderButton5.BeginSettings<Border>()
                    .SetTextPadding<Border>(0, 0, new Thickness(4))
                    .SubscribeMouseSingleClickOnBorder<Border>((o1, ev1) =>
                    {   //CLOSE DIALOG
                        if (canVas.IsCompositeOnParent<Canvas>(dialogGuid))
                        {
                            FasterWPF.Selectors.Select.DisposeEventsOnAllParents(stkPanel1);
                            canVas.RemoveByKey<Canvas>(dialogGuid);
                            resultCallback(false);
                        }
                    })
                    .EndSettings<Border>();

                borderButton5.SetMouseOverColorOnBorder(buttonOnMouseOverColors);

                borderButton5.BeginComposite()
                    .AddText<Border>(0, 0, "  No  ")
                    .EndComposite<Border, BorderArgs>(null);
            
                DockPanel ccPanel5 = new DockPanel();
                ccPanel5.Initialize(width, 33.00, ContainerType.ContentControlPanel);
                ccPanel5.LastChildFill=false;

                Rectangle shim = new Rectangle();
                shim.Fill = Brushes.Transparent;
                shim.Stroke = Brushes.Transparent;
                shim.Width = 10.0;
                shim.Height = 1.0;

                ccPanel5.BeginComposite()
                         .AddExisting<Rectangle, DockPanel>(0, 0, shim)
                         .EndComposite<DockPanel, DockArgs>(new DockArgs(Dock.Right));    

                ccPanel5.BeginComposite()
                         .AddExisting<Border, DockPanel>(0, 0, borderButton5)
                         .EndComposite<DockPanel, DockArgs>(new DockArgs(Dock.Right));                
                
                ccPanel5.BeginComposite()
                        .AddExisting<Border, DockPanel>(0, 0, borderButton3)
                        .EndComposite<DockPanel, DockArgs>(new DockArgs(Dock.Right));
            
                //Capture ESC Key Input and Close Dialog
                Action<EventPattern<KeyEventArgs>> keyInputs = (eventPattern) =>
                    {
                        //CLOSE DIALOG
                        if (eventPattern.EventArgs.Key == Key.Escape) 
                        {                              
                             if (canVas.IsCompositeOnParent<Canvas>(dialogGuid))
                             {
                                 FasterWPF.Selectors.Select.DisposeEventsOnAllParents(stkPanel1);
                                    canVas.RemoveByKey<Canvas>(dialogGuid);
                                    resultCallback(false);
                             }
                        }
                        if (eventPattern.EventArgs.Key == Key.Enter)
                        {
                            if (canVas.IsCompositeOnParent<Canvas>(dialogGuid))
                            {
                                FasterWPF.Selectors.Select.DisposeEventsOnAllParents(stkPanel1);
                                canVas.RemoveByKey<Canvas>(dialogGuid);
                                resultCallback(true);
                            }
                        }
                    };

                FasterWPF.CommonExt.SubscribeEventOnParent(stkPanel1, "PreviewKeyDown", keyInputs);
            
                stkPanel1.BeginComposite()
                         .AddExisting<DockPanel, StackPanel>(0, 2, ccPanel5)
                         .EndComposite<StackPanel, StackPanelArgs>(null);

                var txtBox1 = FasterWPF.Selectors.Select.SelectFrameworkElementsByType<TextBox>(stkPanel1).First();

                System.Windows.Forms.SendKeys.SendWait("{Tab}");
                Keyboard.Focus(txtBox1);
                txtBox1.Focus();

            }

            return brd1;
        }

    }//end of class
}//end of namespace
